<?php

namespace App\Http\Controllers\Admin;

use DB;
use Illuminate\Support\Facades\Redis;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Sate;
use App\Http\Controllers\Api\CommonApi;
use App\Http\Requests\Sateupdate;

/**
 * 站点管理控制器
 * @author [wt1ao] [<424356452@qq.com>]
 */
class Systembase extends Controller
{
    /**
     * 显示编辑站点管理
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $sate = Sate::find(1);

        return view('Admin.system.system-base', ['sate' => $sate]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return 返回成功或者失败
     */
    public function update(Sateupdate $request)
    {
        //实例化Api
        $common = new CommonApi;

        $id = $request->input('id');

        //判断用户是否修改了小图标
        if ($request->file('icon')) {

          $resicon = $common->uploadToQiNiu($request, 'icon');
          //判断是否上传成功
          if ( $resicon['status'] == 4044 ) {

            return redirect()->route('system.index')->with('message', '小图标上传失败');

          }

          $data['icon'] = $resicon['data']['key'];

          // $common->deleteToQiNiu('shop_sates', 'icon', $id);

        }

        //判断用户是否修改了网站logo
        if ($request->file('logo')) {

          $reslogo = $common->uploadToQiNiu($request, 'logo');

          //判断LOGO 是否上传成功
          if ( $reslogo['status'] == 4044 ) {

            // return redirect()->route('system.index')->with('message', 'logo上传失败');

          }

          $data['logo'] = $reslogo['data']['key'];

          $common->deleteToQiNiu('shop_sates', 'logo', $id);

        }

        //接收数据
        $data['name'] = $request->input('name');
        $data['keyword'] = $request->input('keyword');
        $data['desc'] = $request->input('desc');
        $data['message'] = $request->input('message');
        $data['record'] = $request->input('record');
        $data['phone'] = $request->input('phone');

        //执行编辑
        $res = Sate::where('id', $id)->update($data);

        if ( $res ) {
          //执行编辑成功之后把修改的内容写进redis
          $sateHashKey = 'Shop_Sate_data_hash';

          Redis::del($sateHashKey);

          $sate = Sate::find(1)->toArray();

          Redis::hMSet($sateHashKey, $sate);

          return redirect()->route('system.index')->with('message', '成功');
        }

        return redirect()->route('system.index')->with('message', '失败');
    }


}
